<?php
class NewsAction extends CommonAction {
	private $pageUrl = '/news/';
	private $cateObject;
	private $cateInfo = array();
	
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('News');
		$this->modelViewObject = D('NewsView');
		$this->cateObject = D('NewsCate');
		$this->getNav(array($this->webConfig['host_url'].'news/'));
    }
	
    public function index(){
		$map = array();

		//解析url
		$pUrl = I('p', '');
		$thisPage = 1;
		
		//id3p3;
		if((substr($pUrl, 0, 1) == 'c' || strpos($pUrl, 'c'))){
			$cateId = intval(substr($pUrl, strpos($pUrl, 'c')+1));
			$map['cate_id'] = $cateId;
			$this->pageUrl .= 'c'.$cateId;	
			
			$this->cateInfo = $this->cateObject->getInfo($cateId);
			
		}
		if((substr($pUrl, 0, 1) == 'p' || strpos($pUrl, 'p'))){
			$thisPage = intval(substr($pUrl, strpos($pUrl, 'p')+1));
		}
		
		//初始值
		$map['status'] = 1;
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage, $this->webConfig['news_page_size']);
		foreach($list as $key=>$val){
			$list[$key]['url'] = setViewUrl('article', $val);	
		}
		$this->assign('list', $list);
		$this->assign('pageUrl', $this->pageUrl);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage, $this->webConfig['news_page_size']);
		$this->assign('page',$page);
		//p($page);
		
		$this->setSeo($this->cateInfo, 'news', $thisPage);
		$this->assign('cateTree', $this->cateObject->getCateTree(array(), array($this->cateInfo['id'])));
		$this->display();
    }
	
	
	public function show(){
		$id = $pUrl = I('id', '');
		$page = 1;
		//id3p3;
		if((substr($pUrl, 0, 2) == 'id' || strpos($pUrl, 'id'))){
			$id = intval(substr($pUrl, strpos($pUrl, 'id')+2));
		}
		if((substr($pUrl, 0, 1) == 'p' || strpos($pUrl, 'p'))){
			$page = intval(substr($pUrl, strpos($pUrl, 'p')+1));
		}	
		
		if($id && ($info = $this->modelViewObject->getInfo($id))){
			//上一篇，下一篇
			if($info['prev'] = $this->modelObject->getInfo(array('id'=>array('LT', $id)), 'id DESC')){
				$info['prev']['url'] = setViewUrl('article', $info['prev']);	
			}
			if($info['next'] = $this->modelObject->getInfo(array('id'=>array('GT', $id)), 'id ASC')){
				$info['next']['url'] = setViewUrl('article', $info['next']);	
			}
			
			$info['nowPage'] = $page;
			$this->assign('info', $info);
			
			$this->setSeo($info);
			$this->modelObject->where(array('id'=>$id))->setInc('views');
			$this->assign('cateTree', $this->cateObject->getCateTree(array(), array($info['cate_id'])));
			$this->display();	
		}else{
			$this->go404();
		}
	}
}